package com.atguigu.java.test1;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author shkstart
 * @create 2021-02-01 22:45
 */
public class URLExer {

    public static void main(String[] args) {
        InputStream is = null;
        FileOutputStream f1 = null;

        try {
            URL url = new URL("http//:localHost:2020/examples/beauty.jpg ？username=tom");

            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            uc.connect();

            is = uc.getInputStream();
            f1 = new FileOutputStream("hello.jpg");
            byte[] bytes = new byte[102];
            int len;
            while ((len = is.read(bytes)) != -1) {
                f1.write(bytes, 0, len);
            }
            System.out.println("下载完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (f1 != null) {
                    f1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
